<template>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="选择一级种类名称:">
          <el-select v-model="pid,value" placeholder="选择一级种类" @change="changeLevel2(value)">
            <el-option value='' label="请选择"></el-option>
            <el-option @click.native="getPid(item.pid)"
                       v-for="item in fileKind"
                       :key="item.id"
                       :label="item.kindName"
                       :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="选择二级种类名称:" v-if="fileKind2!=null&&fileKind2!=''">
          <el-select v-model="value2" placeholder="选择二级种类" @change="changeLevel3(value2)">
            <el-option value='' label="请选择"></el-option>
            <el-option
                v-for="item in fileKind2"
                :key="item.id"
                :label="item.kindName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择三级种类名称:" v-if="fileKind3!=null&&fileKind3!=''">
          <el-select v-model="value3" placeholder="选择三级种类">
            <el-option value='' label="请选择"></el-option>
            <el-option
                v-for="item in fileKind3"
                :key="item.id"
                :label="item.kindName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="init">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--编号,名称,档次级别,1级分类,二级分类,三级分类,用途类型,产品经理  -->
    <div>
      <el-table
          :data="fileData"
          border
          style="width: 100%">
        <el-table-column
            fixed
            prop="productId"
            label="产品名"
            width="150">
        </el-table-column>
        <el-table-column
            prop="productName"
            label="产品编号 "
            width="120">
        </el-table-column>
        <el-table-column
            prop="procedureDescribe"
            label="设计要求"
            width="120">
        </el-table-column>
        <el-table-column
            prop="costPriceSum"
            label="工时总成本"
            width="120">
        </el-table-column>
        <el-table-column
            label="操作"
            width="180">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="medium">变更</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页    -->
    <div class="block">
      <el-pagination
          @size-change="handleSizeChange"
          :current-page="Number(pageNum)"
          :page-sizes="[2, 4, 8, 16]"
          :page-size="Number(pageSize)"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)">
      </el-pagination>
    </div>


    <!-- 弹出框   -->
    <div>
      <el-dialog title="工序设计" :visible.sync="dialogVisible" :before-close="handleDialogClose">
        <el-form v-model="form">
          <el-form-item>
            <!--          添加再次打开一个表格-->
            <el-button type="primary" icon="el-icon-edit" @click="addProcess"><label>添加工序</label></el-button>
            <br>

            <el-button type="danger" icon="el-icon-delete" @click="removeProcess"><label>删除工序</label></el-button>
          </el-form-item>
          <el-form-item label="设计编号:">
            <b v-text="form.designId"></b>
          </el-form-item>
          <el-form-item label="产品名称:">
            <b v-text="form.productName"></b>
          </el-form-item>
          <el-form-item label="产品编号:">
            <b v-text="form.productName"></b>
          </el-form-item>
          <!--          表格-->
          <div>
            <el-table
                :data="productionData"
                style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
              <el-table-column
                  type="selection"
                  width="55">
              </el-table-column>
              <el-table-column
                  label="工序名称:"
                  width="180"
                  prop="procedureName">
              </el-table-column>
              <el-table-column
                  label="工序编号"
                  width="180"
                  prop="procedureId">
              </el-table-column>
              <el-table-column
                  label="描述"
                  width="180"
                  prop="procedureDescribe">
              </el-table-column>
              <el-table-column
                  label="工时数"
                  width="180">
                <template slot-scope="scope">
                  <el-input v-model="productionData[scope.$index].labourHourAmount"/>
                </template>
              </el-table-column>
              <el-table-column
                  label="工时单位"
                  width="180">
                <template slot-scope="scope">
                  <el-input v-model="productionData[scope.$index].amountUnit"/>
                </template>
              </el-table-column>
              <el-table-column
                  label="单位工时成本"
                  width="180">
                <template slot-scope="scope">
                  <el-input v-model="productionData[scope.$index].costPrice"/>
                </template>
              </el-table-column>
            </el-table>
          </div>

          <el-form-item label="此产品登记人:" label-width="100px">
            <label v-model="form.register"><b>{{ form.register }}</b></label>

            <label style="position: relative;left:300px">登记时间:
              <b>
                <label v-text="form.createTime"></label>
              </b>
            </label>

          </el-form-item>
          <el-form-item label="设计要求:">
            <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.procedureDescribe">
            </el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateFileProduction">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <div>
      <div>
        <!--  添加工序弹出框-->
        <el-dialog title="添加工序" :visible.sync="dialogVisible2" :before-close="handleDialogClose2">
          <!--    查询-->

          <!--  表格  -->
          <el-table
              :data="productionProcessData"
              border
              style="width: 100%">
            <el-table-column
                fixed
                prop="procedureId"
                label="工编号"
                width="150">
            </el-table-column>
            <el-table-column
                prop="procedureName"
                label="工序名称"
                width="120">
            </el-table-column>
            <el-table-column
                prop="procedureDescribe"
                label="工序描述"
                width="120">
            </el-table-column>
            <el-table-column
                label="操作"
                width="100">
              <template slot-scope="scope">
                <el-button @click="handleClick2(scope.row)" type="text" size="medium">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!--    分页-->
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import request from "../../../request.js"

export default {

  name: "FileProductionUpdate",
  data() {
    return {
      productionProcessData: [
        {
          procedureId: '01',
          procedureName: '组装',
          procedureDescribe: '无'
        },
        {
          procedureId: '02',
          procedureName: '测试',
          procedureDescribe: '无'
        },
        {
          procedureId: '03',
          procedureName: '包装',
          procedureDescribe: '无'
        }
      ],
      form: {},
      dialogVisible2: false,
      multipleSelection: [],
      dialogVisible: false,
      formInline: {
        user: '',
        region: ''
      },
      pid: '',
      value: '',
      value2: '',
      value3: '',
      fileKind: [],
      fileKind2: [],
      fileKind3: [],
      //查询数据
      pageNum: '',
      pageSize: '',
      name: '',
      total: '',
      //初始化展示数据
      fileData: [],
      productionData: [],
      //  productionDetailedData:[]
    }
  },
  methods: {
    updateFileProduction() {
      this.form.updateTime = '';
      for (let i = 0; i < this.productionData.length; i++) {
        this.productionData[i].id = '';
      }
      let map = {
        'procedure': this.form,
        'details': this.productionData
      }
      axios.post("http://localhost:8150/design-procedure/updateProcedure", map).then(data => {
        if (data.data.flag==false) {
          this.$message({
            showClose: true,
            message: data.data.message,
            type: data.data.flag == true ? "success" : "error"
          })
          return;
        }

        if (data.data.flag) {
          this.getSelectValue();
          this.init();
        }
        this.$message({
          showClose: true,
          type: data.data.flag == true ? 'success' : "error",
          message: data.data.message
        });
        this.dialogVisible = false;
      }).catch(e => {
        alert("错误信息为:" + e)
      })
    },
    handleClick2(row) {
      let data = {
        procedureId: '',
        procedureName: '',
        procedureDescribe: ''
      }
      data.procedureId = row.procedureId;
      data.procedureName = row.procedureName;
      data.procedureDescribe = row.procedureDescribe;
      data.labourHourAmount = '';
      data.amountUnit = '';
      data.costPrice = '';
      this.productionData.push(data);
      this.dialogVisible2 = false;
    },
    handleDialogClose2() {
      this.dialogVisible2 = false;
    },
    addProcess() {
      this.dialogVisible2 = true;
    },
    removeProcess() {
      let multipleSelection = this.multipleSelection;
      let productionData = this.productionData;
      for (let j = 0; j < multipleSelection.length; j++) {
        for (let i = 0; i < productionData.length; i++) {
          if (productionData[i].procedureId == multipleSelection[j].procedureId) {
            productionData.splice(i, 1);
          }
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDialogClose() {
      this.dialogVisible = false;
    },
    handleClick(row) {
      this.dialogVisible = true;
      axios.get("http://localhost:8150/design-procedure/queryById", {
        params: {
          id: row.productId
        }
      }).then(data => {
        if (data.data.flag==false) {
          this.$message({
            showClose: true,
            message: data.data.message,
            type: data.data.flag == true ? "success" : "error"
          })
          return;
        }
        this.form = data.data.data.procedure;
        this.productionData = data.data.data.list;
      })
    },
    //初始化数据
    init() {
      axios.get("http://localhost:8150/design-procedure/queryProcedure", {
        params: {
          'firstId': this.value,
          'secondId': this.value2,
          'threeId': this.value3,
          'pageNum': this.pageNum,
          'pageSize': this.pageSize,
          'checkTag': 'S001-1'
        }
      }).then(data => {
        if (data.data.flag==false) {
          this.$message({
            showClose: true,
            message: data.data.message,
            type: data.data.flag == true ? "success" : "error"
          })
          return;
        }
        this.fileData = data.data.data.records;
        this.pageNum = data.data.data.current;
        this.pageSize = data.data.data.size;
        this.total = data.data.data.total;
      }).catch(e => {
        alert("错误信息为:" + e)
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      //  console.log(`每页 ${val} 条`);
      this.init();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      //console.log(`当前页: ${val}`);
      this.init();
    },
    changeLevel2(id) {
      // console.log("value:" + id)
      let a = 0;
      for (let i = 0; i < this.fileKind.length; i++) {
        for (let j = 0; j < this.fileKind[i].list.length; j++) {
          if (id === this.fileKind[i].list[j].pid) {
            this.fileKind2[a] = this.fileKind[i].list[j];
            a++;
          } else {
            this.fileKind2 = [];
            this.fileKind3 = [];
            this.value2 = '';
            this.value3 = '';
          }
        }

      }
    }, getPid(id) {
      this.pid = id;
    },//获取下拉框的值
    getSelectValue() {
      axios.get("http://localhost:8150/config-file-kind/displayType").then(data => {
        this.fileKind = data.data.data;
        this.fileKind11 = data.data.data;
      }).catch(e => {
        alert("错误信息为:" + e)
      });
    },
    changeLevel3(id) {
      //  console.log(id)
      let a = 0;
      for (let i = 0; i < this.fileKind2.length; i++) {
        for (let j = 0; j < this.fileKind2[i].list.length; j++) {
          if (id == this.fileKind2[i].list[j].pid) {
            // console.log(this.fileKind2[i].list[j].pid)
            this.fileKind3[a] = this.fileKind2[i].list[j];
            a++;
          } else {
            this.fileKind3 = [];
            this.value3 = '';
          }
        }
      }
    }
  }, created() {
    this.getSelectValue();
    this.init();
  }
}
</script>

<style scoped>

</style>